<?php

namespace addons\shopro\helpers;

use Matrix\Exception;

/**
 * @Author ycr <E-mail:yuncran@126.com>\
 */
class EncryptionHelper
{

    /**
     * 解密url
     * @param array $paramArr
     * @param $secret
     * @param $signName
     * @return bool
     * @throws \think\Exception
     * @Author ycr <E-mail:yuncran@126.com>
     */
    public static function decodeUrlParam(array $paramArr, $secret, $signName = 'sign')
    {
        $sign = $paramArr[$signName];
        unset($paramArr[$signName]);

        ksort($paramArr);
        $signStr = '';
        foreach ($paramArr as $key => $val) {
            $signStr .= $key . '=' . urlencode($val) . '&';
        }
        // 去掉最后一个&
        $signStr = substr($signStr, 0, strlen($signStr) - 1);

        // 排好序的参数加上secret,进行md5
        $signStr .= $secret;
        if (strtolower(md5($signStr)) !== $sign) {
            throw new \think\Exception('签名错误',10014);
        }
        return true;
    }

}